//
//  SinaStatusViewModel.swift
//  SinaWeibo
//
//  Created by 王朝阳 on 2016/10/27.
//  Copyright © 2016年 wangzhaoyang. All rights reserved.
//

import UIKit

class SinaStatusViewModel: NSObject {
    //viewModel 管理SinaStatusModel处理数据
    var statusModel: SinaStatusModel?{
        didSet{
            //会员等级赋值
            mbrankImage = dealMbrankImage(mbrank: statusModel?.user?.mbrank ?? 0)
            //微博认证赋值
            verifiedImage = dealVerifiedImage(verified: statusModel?.user?.verified ?? -1)
            //转发微博拼接
            retweetContentText = "@\(statusModel?.retweeted_status?.user?.name ?? ""):\(statusModel?.retweeted_status?.text ?? "")"
            // 转发个数
            repostsCountStr = dealCount(count: statusModel?.reposts_count ?? 0, title: "转发")
            // 评论个数
            commentsCountStr = dealCount(count: statusModel?.comments_count ?? 0, title: "评论")
            // 赞个数
            attitudesStr = dealCount(count: statusModel?.attitudes_count ?? 0, title: "赞")
            //处理来源数据
            handleSource(source: statusModel?.source ?? "")
        }
    }
    //会员等级属性
    var mbrankImage: UIImage?
    //微博认证
    var verifiedImage: UIImage?
    //转发微博显示的内容
    var retweetContentText: String?
    // 转发个数
    var repostsCountStr: String?
    // 评论个数
    var commentsCountStr: String?
    // 赞个数
    var attitudesStr: String?
    //来源属性
    var sourceStr: String?
    //时间格式处理
    var timeStr: String? {
        //把时间对象格式化成显示的时间字符串
        guard let createAt = statusModel?.created_at else {
            return nil
        }
        
        let dt = DateFormatter()
        //指定本地化信息
        dt.locale = Locale(identifier: "en_US")
        if isThisYear(creatAt: createAt) {
            
            //判断是否是今天,昨天,还是其他
            let calender = Calendar.current
            if calender.isDateInToday(createAt) {
                //判断是否是一分钟之前,一小时之前还是其他
                let timeInterval = abs(createAt.timeIntervalSinceNow)
                if timeInterval < 60 {
                    return "刚刚"
                }else if timeInterval < 3600 {
                    let result = Int(timeInterval) / 60
                    return "\(result)分钟前"
                }else {
                    let result = Int(timeInterval) / 3600
                    return "\(result)小时前"
                }
            }else if calender.isDateInYesterday(createAt) {
                dt.dateFormat = "昨天 HH:mm"
            }else {
                dt.dateFormat = "MM-dd HH:mm"
            }
        }else {
            //不是今年
            dt.dateFormat = "yyyy-MM-dd HH:mm"
        }
        return dt.string(from: createAt)
    }
    
    //根据发微博的时间判断是否是今年
    private func isThisYear(creatAt: Date) -> Bool {
        
        let dt = DateFormatter()
        //指定格式化方式
        dt.dateFormat = "yyyy"
        //指定本地化信息
        dt.locale = Locale(identifier: "en_US")
        //获取发微博的年份
        let creatAtYear = dt.string(from: creatAt)
        
        //获取当前时间的年份
        let currentDateYear = dt.string(from: Date())
        
        return creatAtYear == currentDateYear
    }
}
//MARK: - 处理转发评论赞
extension SinaStatusViewModel {
    func dealCount(count: Int, title: String) -> String{
        if count <= 0 {
            return title
        }else if count > 0 && count < 10000{
            return "\(count)"
        }else {
            let c = CGFloat(count/1000)/10
            var cStr = "\(c)万"
            
            // 如果cStr 包含.0
            if cStr.contains(".0") {
                // 字符串替换
                cStr = cStr.replacingOccurrences(of: ".0", with: "")
            }
            return cStr
        }
    }

    
}
extension SinaStatusViewModel {
   
    //处理微博认证
    func dealVerifiedImage(verified: Int) -> UIImage?{
        switch verified {
        case 1:
            return UIImage(named: "avatar_vip")
        case 2,3,5:
            return UIImage(named: "avatar_enterprise_vip")
        case 220:
            return UIImage(named: "avatar_grassroot")
        default:
            return UIImage(named: "avatar_vgirl")
        }
    }
    //处理微博用户等级
    func dealMbrankImage(mbrank: Int) -> UIImage?{
        if mbrank > 0 && mbrank < 7 {
            return UIImage(named: "common_icon_membership_level\(mbrank)")
        }else {
            return UIImage(named: "common_icon_membership")
        }
    }
}
//MARK: - 处理微博数据来源
extension SinaStatusViewModel {
    //处理来源数据
    func handleSource(source: String) {
        
        if let startRange = source.range(of: "\">"), let endRange = source.range(of: "</") {
            //获取开始光标的位置
            let startIndex = startRange.upperBound
            //获取范围的结束光标位置的索引
            let endIndex = endRange.lowerBound
            //获取指定范围的字符串
            sourceStr = "来自:" + source.substring(with: startIndex..<endIndex)
        }
    }
}
